package com.rlyy.common.response;


import com.rlyy.common.exception.BaseRuntimeException;
import com.rlyy.common.enums.CommonEnum;
import com.rlyy.common.enums.ExceptionEnum;

/**
 * @author yuanqinglong
 * @since 2020/9/10 14:12
 */
public class ResultUtil {


    /**
     * 请求成功返回请求数据
     *
     * @param data 返回数据
     * @return .
     */
    public static Result success(Object data) {
        Result result = new Result();
        result.setData(data);
        result.setMsg(CommonEnum.SUCCESSFULLY.getMsg());
        result.setCode(String.valueOf(CommonEnum.SUCCESSFULLY.getCode()));
        return result;
    }

    /**
     * 根据布尔值判断返回
     * @param isSuccess
     * @return
     */
    public static Result success(boolean isSuccess) {
        return isSuccess ? success(): error();
    }

    /**
     * 请求成功返回请求数据
     *
     * @param data 返回数据
     * @param file 文件参数
     * @return .
     */
    public static Result success(Object data, Object file) {
        Result result = new Result();
        result.setData(data);
        result.setMsg(CommonEnum.SUCCESSFULLY.getMsg());
        result.setCode(String.valueOf(CommonEnum.SUCCESSFULLY.getCode()));
        return result;
    }

    /**
     * 请求成功
     *
     * @return .
     */
    public static Result success() {
        Result result = new Result();
        result.setMsg(CommonEnum.SUCCESSFULLY.getMsg());
        result.setCode(String.valueOf(CommonEnum.SUCCESSFULLY.getCode()));
        return result;
    }


    public static Result error() {
        Result result = new Result();
        result.setMsg(CommonEnum.FAILED.getMsg());
        result.setCode(String.valueOf(CommonEnum.FAILED.getCode()));
        return result;
    }


    /**
     * 返回自定义异常
     *
     * @param exceptionEnum 异常信息
     * @return .
     */
    public static Result error(ExceptionEnum exceptionEnum) {
        Result result = new Result();
        result.setMsg(exceptionEnum.getMsg());
        result.setCode(exceptionEnum.getCode().toString());
        return result;
    }


    public static Result error(String code, String msg) {
        Result result = new Result();
        result.setMsg(msg);
        result.setCode(code);
        return result;
    }

    public static Result error(BaseRuntimeException exception) {
        Result result = new Result();
        result.setMsg(exception.getMessage());
        result.setCode(exception.getCode());
        return result;
    }
}
